<?php
# vim: syntax=php tabstop=4 softtabstop=0 noexpandtab laststatus=1 ruler

/**
 *
 * @author yangfeng <81177622@qq.com>
 * @copyright 2018-2020 yangfeng
 * @version 20180506
 */
//echo exec('whoami');
//echo shell_exec("id -a");
if($_GET["action"]=="fix")
	{
		$o = "" ;
		$cmd = 'sudo fix_permissions 2>&1';
		exec($cmd, $o, $rc);
		if ($rc == 0) {
			$output['code'] = $rc;
			$output['messages'] = "Fix permissions Success.";
			$output['status'] = 'Success';
		} else {
			$output['code'] = $rc;
			$output['messages'] = "Permission denied.";
			$output['status'] = 'Failed';
		};
	}
	elseif($_GET["action"]=="dl")
	{
		$o = "" ;
		$cmd = 'grep -i -o "\$node_templates\[\$templ\] = \$desc.TEMPLATE_DISABLED  ;" /opt/unetlab/html/includes/init.php 2>&1';
		exec($cmd, $o, $rc);
		if ($rc == 0) {
			$cmd = 'sudo hide_device 2>&1';
			exec($cmd, $o, $rc);
			$output['code'] = 200;
			$output['rc'] = $rc;
			if ($rc == 0) {
				$output['messages'] = "Hide Invalid Device Success.";
				$output['status'] = 'Success';
			}else{
				$output['messages'] = "Permission denied.";
				$output['status'] = 'Failed';
			}
		} else {
			$cmd = 'grep -i -o "\$node_templates\[\$templ\] = \$desc...missing.;" /opt/unetlab/html/includes/init.php 2>&1';
			exec($cmd, $o, $rc);
			if ($rc == 0) {
				$cmd = 'sudo hide_device 2>&1';
				exec($cmd, $o, $rc);
				$output['code'] = 200;
				$output['rc'] = $rc;
				if ($rc == 0) {
					$output['messages'] = "Hide Invalid Device Success.";
					$output['status'] = 'Success';
				}else{
					$output['messages'] = "Permission denied.";
					$output['status'] = 'Failed';
				}
			}else{
				$cmd = 'sudo show_device 2>&1';
				exec($cmd, $o, $rc);
				$output['code'] = 201;
				$output['rc'] = $rc;
				if ($rc == 0) {
					$output['messages'] = "Show Invalid Device Success.";
					$output['status'] = 'Success';
				}else{
					$output['messages'] = "Permission denied.";
					$output['status'] = 'Failed';
				}
			}
		};	 	
	}
	elseif($_GET["action"]=="iol")
	{
		$o = "" ;
		$cmd = 'sudo IOUKeygen 2>&1';
		exec($cmd, $o, $rc);
		if ($rc == 0) {
			$output['code'] = $rc;
			$output['messages'] = "Generate IOU License Success.";
			$output['status'] = 'Success';
		} else {
			$output['code'] = $rc;
			$output['messages'] = "Permission denied.";
			$output['status'] = 'Failed';
		};	 	
	}
	elseif($_GET["action"]=="cn")
	{
		$o = "" ;
		$cmd = 'sudo language_zh 2>&1';
		exec($cmd, $o, $rc);
		if ($rc == 0) {
			$output['code'] = $rc;
			$output['messages'] = "Set Language to Chinese simplified Success.";
			$output['status'] = 'Success';
		} else {
			$output['code'] = $rc;
			$output['messages'] = "Permission denied.";
			$output['status'] = 'Failed';
		};
	}
	elseif($_GET["action"]=="en")
	{
		$o = "" ;
		$cmd = 'sudo language_en 2>&1';
		exec($cmd, $o, $rc);
		if ($rc == 0) {
			$output['code'] = $rc;
			$output['messages'] = "Set Language to English Success.";
			$output['status'] = 'Success';
		} else {
			$output['code'] = $rc;
			$output['messages'] = "Permission denied.";
			$output['status'] = 'Failed';
		};
	}elseif($_GET["action"]=="str"){
			$cmd = 'sudo connector ' .str;
			exec($cmd, $o, $rc);
			if ($rc == 0) {
				$output['code'] = $rc;
				$output['messages'] = "Set connectors to straight Success.";
				$output['status'] = 'Success';
			} else {
				$output['code'] = $rc;
				$output['messages'] = "Permission denied.";
				$output['status'] = 'Failed';
			};
	}elseif($_GET["action"]=="flo"){
			$cmd = 'sudo connector ' .flo;
			exec($cmd, $o, $rc);
			if ($rc == 0) {
				$output['code'] = $rc;
				$output['messages'] = "Set connectors to straight Success.";
				$output['status'] = 'Success';
			} else {
				$output['code'] = $rc;
				$output['messages'] = "Permission denied.";
				$output['status'] = 'Failed';
			};
	}elseif($_GET["action"]=="bez"){
			$cmd = 'sudo connector ' .bez;
			exec($cmd, $o, $rc);
			if ($rc == 0) {
				$output['code'] = $rc;
				$output['messages'] = "Set connectors to straight Success.";
				$output['status'] = 'Success';
			} else {
				$output['code'] = $rc;
				$output['messages'] = "Permission denied.";
				$output['status'] = 'Failed';
			};
	}else{
		$output['code'] = '400';
		$output['messages'] = "Unknown Error. Why did API doesn't respond?";
		$output['status'] = 'Failed';
	}

?>
<?php echo json_encode($output)?>
